package com.zwps.biz.controller.listener.rabbitmq;

import com.rabbitmq.client.Channel;
import com.zwps.biz.domain.extra.rabbitmq.define.FileDefine;
import com.zwps.biz.domain.service.FileProcessService;
import com.zwps.common.core.enums.MessageTypeEnum;
import com.zwps.common.core.service.LogService;

import lombok.AllArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.io.IOException;
import static org.springframework.amqp.core.ExchangeTypes.TOPIC;

/**
 * 文件处理消息
 * 
 * @ClassName: FileProcessMQListener
 * @Description:
 * @author lyh
 * @date 2023年8月20日 下午6:47:23
 *
 */
@Component
@AllArgsConstructor
@ConditionalOnProperty("spring.rabbitmq.enable")
public class FileProcessMQListener {

    private FileProcessService fileProcessService;

    private LogService logService;

    /**
     * 图片压缩
     * 
     * @Title: pictureProcessQueueListener
     * @Description:
     * @param channel
     * @param message
     * @throws IOException void
     * @author lyh
     * @date 2023年8月20日 下午6:47:33
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = FileDefine.PICTURE_COMPRESS_QUEUE, durable = "true"), // 配置队列
            exchange = @Exchange(value = FileDefine.EXCHANGE, type = TOPIC), // 配置交换机，交换机类型
            key = { FileDefine.PICTURE_COMPRESS_ROUTE } // 配置路由
    ), ackMode = "MANUAL")
    public void pictureProcessQueueListener(Channel channel, Message message) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        logService.consumeRecord((body, logMessage) -> {
            fileProcessService.pictureCompress(body);
        }, message, MessageTypeEnum.FILE_IMAGE_COMPRESS);
    }

    /**
     * 视频处理消息
     * @Title: videoProcessQueueListener
     * @Description: 
     * @param channel
     * @param message
     * @throws IOException
     * @author lyh
     * @date 2023年10月26日 下午7:48:12
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = FileDefine.VIDEO_PROCESS_QUEUE, durable = "true"), // 配置队列
            exchange = @Exchange(value = FileDefine.EXCHANGE, type = TOPIC), // 配置交换机，交换机类型
            key = { FileDefine.VIDEO_PROCESS_ROUTE } // 配置路由
    ), ackMode = "MANUAL")
    public void videoProcessQueueListener(Channel channel, Message message) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        logService.consumeRecord((body, logMessage) -> {
            fileProcessService.videoProcess(body);
        }, message, MessageTypeEnum.FILE_IMAGE_COMPRESS);
    }

}
